package entity.lemming.animation;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import entity.Entity;
import entity.lemming.Lemming;
import entity.terrain.Terrain;
//import level.Level;

/**
 * A lemming that falls either from level entrance or edge of terrain.
 * Lemming dies on impact if falls is above a certain distance
 * 
 * @author Olga Murdoch
 */
public class Faller extends Lemming {
	
	/** The helper variables allowing implementation of max "safe" fall height. */
	private int maxfall = img.getHeight()*6;
	private int currfall;
	private int start;
	private boolean die;

	/**
	 * 
	 * @param x the starting x-coordinate.
	 * @param y the starting y-coordinate.
	 */
	/*@ requires 0 <= x && 0 <= y;
		assignable speed_X && speed_Y && energy;
		ensures speed_X == 0 && speed_Y > 0 && energy == 100;
	 */
	public Faller(int x, int y){
		super("resource/graphics/lemming/faller/faller_1.gif",7, x, y);
		speed_X = 0;
		speed_Y = 1;
		energy = 100;
		// set the helper variables
		start = y;
		currfall = start;
	}
	
	/**
	 * Create a new Faller with the given center point
	 * 
	 * @param center point of the Lemming
	 */
	public Faller(Point center){
		super("resource/graphics/lemming/faller/faller_1.gif",7, center);
		speed_X = 0;
		speed_Y = 1;
		energy = 100;
		// set the helper variables
		start = y;
		currfall = start;
	}
	
	/** 
	 * Checks for a collision with a certain Entitie
	 * Left
	 * @param other another Entity ie Terrain.
	 * @return true if a collision has occured.
	 */
	public /*@ pure @*/ boolean collision(Lemming other) {
		/*Rectangle me = getBounds();
		if (other instanceof Terrain){
			if (me.intersects(other.getBounds())){
				//return true;
			}
		}*/
		return false;
	}

	/** Request that this lemming perform the appropiate action/movement. */
	
	//	@ ensures y = \old(y) - speed_Y && energy == 100;
	public void move(Terrain terrain) {
		// calculate the next y coordinate
		Rectangle lemming = getBounds();
		currfall = y;
		
		if (lemming.intersects(terrain.getBounds())){
			BufferedImage image = ((Terrain)terrain).getImage().getBufferedImage();
				int black = -16777216; // black that represents the background
				int black2 = -16776960;
				
				try{
					for (int curr_x = getX(); curr_x < getX() + lemming.width; curr_x++){
						int color = 0;
						
							color = image.getRGB(curr_x, y+lemming.height);
							//System.out.println(color);
							// if we have a pixel to stand on
							if (color != black){
								if (color != black2){
									if (currfall-start > maxfall){
										
										crush = true;
									}
									else{
										energy = 0;
									}
								}
								break;
							}
					}
				}catch(ArrayIndexOutOfBoundsException e){
					
				}
				}
		
		if (energy != 0){
			y= y+speed_Y;
			update();
			energy--;
		}
	}
	
	/** Override turnAround. */
	public void turnAround(){
		if (old_speed_X < 0){
			generateFrames("resource/graphics/lemming/faller/fallerLeft_1.gif",7);
		}
		else {
			generateFrames("resource/graphics/lemming/faller/faller_1.gif",7);
		}
	}

}
